package xt.toolkit.rpc.core;

import xiaojian.toolkit.netty.handler.codec.http.request.HttpRequestExt;
import xiaojian.toolkit.netty.util.NettyConstants;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;

public class SendHepler {

	public static void sendResponse(HttpRequestExt request,
			HttpResponse response) {
		Channel channel = (Channel) request
				.getProperty(NettyConstants.PROPERTY_CHANNEL);

		sendResponse(channel, response);

	}

	public static void sendResponse(Channel channel, HttpResponse response) {
		ChannelFuture future = channel.writeAndFlush(response);
		if (!HttpHeaders.isKeepAlive(response)
				|| !response.headers().contains(
						HttpHeaders.Names.CONTENT_LENGTH)) {
			// no content
			future.addListener(ChannelFutureListener.CLOSE);
		}
	}

}
